﻿using System;

namespace SearchInsertPosition
{
    public class Solution
    {
        public int SearchInsert(int[] nums, int target)
        {
            int first = 0;
            int last = nums.Length - 1;

            while (true)
            {
                int mid = (first + last) / 2;
                if (nums[mid] == target)
                {
                    return mid;
                }
                else if (nums[mid] < target)
                {
                    first = mid;
                }
                else
                {
                    last = mid;
                }
            }
        }
    }
}
